package com.zimu.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zimu.api.mapper.InterfaceInfoMapper;
import com.zimu.api.mapper.UserInterfaceInfoMapper;
import com.zimu.api.service.AnalysisInterfaceService;
import com.zimu.common.model.entity.InterfaceInfo;
import com.zimu.common.model.entity.UserInterfaceInfo;
import com.zimu.common.model.vo.InterfaceInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AnalysisInterfaceServiceImpl implements AnalysisInterfaceService {

    @Resource
    InterfaceInfoMapper interfaceInfoMapper;
    @Resource
    UserInterfaceInfoMapper userInterfaceInfoMapper;
    @Override
    public List<InterfaceInfoVo> getTopInterfaceInvoke(HttpServletRequest request) {
        List<UserInterfaceInfo> topUserInterfaceInfo = userInterfaceInfoMapper.getTopInvokeNumInterfaceInfo();
        QueryWrapper<InterfaceInfo> infoQueryWrapper = new QueryWrapper<>();
        infoQueryWrapper.in("id",
                topUserInterfaceInfo.stream().map(UserInterfaceInfo::getInterfaceInfoId)
                        .collect(Collectors.toList()) );
        List<InterfaceInfo> list = interfaceInfoMapper.selectList(infoQueryWrapper);
        List<InterfaceInfoVo> interfaceInfoVos = list.stream().map(o -> {
            InterfaceInfoVo interfaceInfoVo = new InterfaceInfoVo();
            BeanUtils.copyProperties(o, interfaceInfoVo);
            Integer totalNum = topUserInterfaceInfo.stream().filter(u -> u.getInterfaceInfoId() == interfaceInfoVo.getId())
                            .collect(Collectors.toList()).get(0).getTotalNum();
            interfaceInfoVo.setTotalNumber( totalNum );
            return interfaceInfoVo;
        }).collect(Collectors.toList());
        return interfaceInfoVos;
    }
}
